package com.hz.house.utils;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * <p> 将日期时间转换为天数记录是否连续登录、连续操作等业务
 * @author 10203
 *
 */
public class DaysIntUtil {
	
	
	/**
	 * 	日期转为天数值
	 * @param yyyy-MM-dd
	 * @return
	 */
	public static int getDays(String yyyy_MM_dd) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d=null;
		try {
			d = sdf.parse(yyyy_MM_dd);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		long t = d.getTime();
		return (int) (t/(24*60*60*1000L));
	}

	/**
	 * 	日期对象转换为天数值
	 * @param time
	 * @return
	 */
	public static int getDays(Date time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = sdf.format(time);
		return getDays(date);
	}
	
	/**
	 * <p> 给定时间的毫秒值转为天数值
	 * @param mills
	 * @return
	 */
	public static int getDays(long mills) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = sdf.format(mills);
		return getDays(date);
	}

	public static final String HH$mm = "HH:mm";
	public static final String MM$dd = "MM-dd";
	public static final String HHmmss = "HHmmss";
	public static final String yyyy$MM$dd="yyyy-MM-dd";
	public static final String yyyy$$MM$$dd="yyyy/MM/dd";
	public static final String yyyy$$MM$$dd$$HH$$mm$$ss ="yyyy/MM/dd HH:mm:ss";
	public static final String yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
	public static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";
	public static final String yyyyMMddHHmm = "yyyyMMddHHmm";
	public static final String yyyyMMdd = "yyyyMMdd";
	public static final String yyyy$MM$dd$HH$mm$ss = "yyyy-MM-dd HH:mm:ss";
	public static final String yyyy$MM$dd$HH$mm = "yyyy-MM-dd HH:mm";
	public static String dateToString(Date date, String formart){
		if (date == null){
			return null;
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat(formart);
		return dateFormat.format(date);
	}

	public static Date parseDate(String str, String formart) {
		if (StringUtils.isEmpty(str)){
			return null;
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat(formart);
		try {
			return dateFormat.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date dateMove(Date date, int day){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_YEAR, day);
		return calendar.getTime();
	}
	//获取一月的总共天数
	public static int getDaysOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
	//获取时间中的日期
	public static int getMonthDayOfDate(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	public static Date getOffsetDay(Date date, int day){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_YEAR, day);
		return calendar.getTime();
	}

	public static int getMonthOfDate(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.MONTH) + 1;
	}


	public static Date dateToNextMinute(Date date, int minute){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MINUTE, minute);
		return c.getTime();
	}

	public static Date getDayStart(Date date){
		if (date == null) return null;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return c.getTime();
	}

	public static Date getDayEnd(Date date){
		if (date == null) return null;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		return c.getTime();
	}

	public static String miniteFormat(int minite){
		int _minite = minite % 60;
		int _hour = minite / 60 % 24;
		int _day = minite / 60 /24;
		String res = "";
		if (_day > 0) res += String.format("%02d天", _day);
		if (_hour > 0) res += String.format("%02d小时", _hour);
		if (_minite > 0) res += String.format("%02d分钟", _minite);
		if (res.equals("")){
			res += "0分钟";
		}
		return res;
	}

	public static String secFormat(long sec){
		long _sec = sec % 60;
		long _minite = sec/ 60 % 60;
		long _hour = sec /60 / 60 % 24;
		long _day = sec / 60/ 60 /24;
		String res = "";
		if (_day > 0) res += String.format("%02d天", _day);
		if (_hour > 0) res += String.format("%02d小时", _hour);
		if (_minite > 0) res += String.format("%02d分钟", _minite);

		if (_sec > 0) res += String.format("%02d秒", _sec);
		if (res.equals("")){
			res += "0秒";
		}
		return res;
	}

	public static String weekDayFormat(int weekDayIndex){
		if (weekDayIndex == 2){
			return "一";
		}else if (weekDayIndex == 3){
			return "二";
		}else if (weekDayIndex == 4){
			return "三";
		}else if (weekDayIndex == 5){
			return "四";
		}else if (weekDayIndex == 6){
			return "五";
		}else if (weekDayIndex == 7){
			return "六";
		}else if (weekDayIndex == 1){
			return "日";
		}else {
			return "";
		}
	}

	/**
	 * 获取指定月的开始日期
	 * @param currentDate
	 * @return
	 */
	public static Date getMonthStartDate(String currentDate){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return getMonthStartDate(sdf.parse(currentDate));
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	public static Date getMonthStartDate(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return c.getTime();
	}

	/**
	 * 获取指定月的结束日期
	 * @param currentDate
	 * @return
	 */
	public static Date getMonthEndDate(String currentDate){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date = sdf.parse(currentDate);
			return getMonthEndDate(date);
		}catch (ParseException e){
			e.printStackTrace();
			return null;
		}
	}

	public static Date getMonthEndDate(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		return c.getTime();
	}
	/**
	 * 计算两个时间之间相差天数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int differentDays(Date date1,Date date2)
	{
		if (Objects.isNull(date1) ||Objects.isNull(date2)){
			return -1;
		}
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
		int day1= cal1.get(Calendar.DAY_OF_YEAR);
		int day2 = cal2.get(Calendar.DAY_OF_YEAR);

		int year1 = cal1.get(Calendar.YEAR);
		int year2 = cal2.get(Calendar.YEAR);
		if(year1 != year2)   //同一年
		{
			int timeDistance = 0 ;
			for(int i = year1 ; i < year2 ; i ++)
			{
				if(i%4==0 && i%100!=0 || i%400==0)    //闰年
				{
					timeDistance += 366;
				}
				else    //不是闰年
				{
					timeDistance += 365;
				}
			}

			return timeDistance + (day2-day1) ;
		}
		else    //不同年
		{
			System.out.println("判断day2 - day1 : " + (day2-day1));
			return day2-day1;
		}
	}
	/**
	 * 	通过时间秒毫秒数判断两个时间的间隔
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int differentDaysByMillisecond(Date date1,Date date2)
	{
		int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
		return days;
	}

	/**
	 * 计算两个时间相差的分钟数
	 * @param start
	 * @param end
	 * @return
	 */
	public static int getDistanceMinutes(Date start, Date end) {
		return (int) ((end.getTime() - start.getTime()) / 60000);
	}
	public static int getDistanceSecend(Date start, Date end) {
		return (int) ((end.getTime() - start.getTime()) / 1000);
	}

	public static int getDayMinitutes(Date date){
		if (date == null){
			return 0;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);

		return c.get(Calendar.HOUR_OF_DAY) * 60 + c.get(Calendar.MINUTE);
	}


	public static void main(String[] args) throws ParseException {
//		long x = 1573219356000l;
			long  x = 1583980843000l;
		System.out.println(new Date(0));
//		x = Long.MAX_VALUE;
		System.out.println(x);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(sdf.format(x));
	}
	
}
